<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>排序的几种方式</title>
  </head>
  <body>
    <script>
      // 最基本的数字排序
      let arr = [1, 6, 9, 5, 3, 4, 7];
      let aa = arr.sort((a, b) => a - b); //sort会改变原数组 ,a-b:从低到高，b-a从高到低
      console.log(aa); // [1,3,4,5,6,7,9]

      // 字符串排序;
      arr1 = ["candy", "jeck", "simon", "lucky", "caane"];
      let a1 = arr1.sort(); // 直接排序,按照首字母排序
      console.log(a1); //[ 'caane', 'candy', 'jeck', 'lucky', 'simon' ]
      //按照第二个字符排序
      let a2 = arr1.sort((a, b) => (a[1] + "").localeCompare(b[1]));
      // let a2 = arr1.sort((a, b) => a[1].localeCompare(b[1]));
      console.log(a2); //[ 'caane', 'candy', 'jeck', 'simon', 'lucky' ]
    </script>
  </body>
</html>
